/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.entity;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import com.googlecode.technorati4j.util.StringUtils;

/**
 * Response entity for cosmos API.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 */
public final class CosmosResponse
    implements Response {

    /**
     * Query url.
     */
    private String url;

    /**
     * Number of inbound blogs.
     */
    private Integer inboundBlogs;

    /**
     * Number of inbound links.
     */
    private Integer inboundLinks;

    /**
     * Start parameter value.
     */
    private Integer rankingStart;

    /**
     * List of items found.
     */
    private List<CosmosResponseItem> items;

    @SuppressWarnings("unchecked")
    public CosmosResponse(Element element) {
        Element result = element.getChild("result");

        url = result.getChildText("url");
        inboundBlogs = StringUtils.toInteger(result.getChildText("inboundblogs"));
        inboundLinks = StringUtils.toInteger(result.getChildText("inboundlinks"));
        rankingStart = StringUtils.toInteger(result.getChildText("rankingstart"));

        items = new ArrayList<CosmosResponseItem>();
        List<Element> _items = element.getChildren("item");

        for (Element item : _items) {
            this.items.add(new CosmosResponseItem(item));
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Integer getInboundBlogs() {
        return inboundBlogs;
    }

    public void setInboundBlogs(Integer inboundBlogs) {
        this.inboundBlogs = inboundBlogs;
    }

    public Integer getInboundLinks() {
        return inboundLinks;
    }

    public void setInboundLinks(Integer inboundLinks) {
        this.inboundLinks = inboundLinks;
    }

    public Integer getRankingStart() {
        return rankingStart;
    }

    public void setRankingStart(Integer rankingStart) {
        this.rankingStart = rankingStart;
    }

    public List<CosmosResponseItem> getItems() {
        return items;
    }

    public void setItems(List<CosmosResponseItem> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        final StringBuilder str = new StringBuilder();
        str.append("url=");
        str.append(this.url);
        str.append("&inboundBlogs=");
        str.append(this.inboundBlogs);
        str.append("&inboundLinks=");
        str.append(this.inboundLinks);
        str.append("&rankingStart=");
        str.append(this.rankingStart);
        str.append("&items=");
        str.append(this.items);

        return str.toString();
    }

}
